home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3 / CHAPTER4 / GETWNDWD.C < prev    next >
C/C++ Source or Header  |  1996-01-13  |  7KB  |  225 lines

  1.  
  2. #include <windows.h>  
  3. #include "GetWndWd.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "My Application"; 
  20.  
  21. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  22.  
  23. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  24.                       LPTSTR lpCmdLine, int nCmdShow)
  25. {
  26.    MSG      msg;
  27.    HWND     hWnd; 
  28.    WNDCLASS wc;
  29.  
  30.    // Register the main application window class.
  31.    //............................................
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    // Create the main application window.
  54.    //....................................
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107.  
  108. LRESULT CALLBACK NewWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 
  109. {
  110.    switch( uMsg )
  111.    {
  112.       case WM_COMMAND :
  113.              switch( LOWORD( wParam ) )
  114.              {  
  115.                 case IDM_TEST :
  116.                       {
  117.                           WORD wValue = GetWindowWord( hWnd, 0 );
  118.                           char szOut[20];
  119.  
  120.                           wsprintf( szOut, "Window Value = %d", wValue );
  121.  
  122.                           MessageBox( hWnd, szOut, lpszAppName, MB_OK );
  123.                       }
  124.                       break;
  125.  
  126.                 case IDM_EXIT :
  127.                          DestroyWindow( hWnd );
  128.                          break;
  129.              }
  130.              break;
  131.  
  132.       default :
  133.              return( DefWindowProc( hWnd, uMsg, wParam, lParam) );
  134.    }
  135.  
  136.    return( 0L );
  137. }
  138.  
  139.  
  140. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  141. {
  142. static HWND hNewWnd = NULL;
  143.  
  144.    switch( uMsg )
  145.    {
  146.       case WM_CREATE:
  147.               {
  148.                   WNDCLASS wc;
  149.  
  150.                   wc.style         = CS_HREDRAW | CS_VREDRAW | CS_PARENTDC; 
  151.                   wc.lpfnWndProc   = (WNDPROC)NewWndProc;        
  152.                   wc.cbClsExtra    = 0;                           
  153.                   wc.cbWndExtra    = sizeof( WORD );    
  154.                   wc.hInstance     = hInst;                   
  155.                   wc.hIcon         = LoadIcon(hInst, lpszAppName); 
  156.                   wc.hCursor       = LoadCursor(NULL, IDC_ARROW); 
  157.                   wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);  
  158.                   wc.lpszMenuName  = lpszAppName;                   
  159.                   wc.lpszClassName = "New Class";  
  160.                   
  161.                   if ( RegisterClass( &wc ) )
  162.                   {
  163.                       hNewWnd = CreateWindow( "New Class", 
  164.                                               "Pop-up Window",
  165.                                               WS_POPUP | WS_VISIBLE |
  166.                                               WS_CAPTION,
  167.                                               10, 50, 150, 150, 
  168.                                               hWnd,
  169.                                               NULL,
  170.                                               hInst, NULL );
  171.                       if ( hNewWnd )
  172.                          SetWindowWord( hNewWnd, 0, 20 );
  173.                   }
  174.               }
  175.               break;
  176.  
  177.       case WM_COMMAND :
  178.               switch( LOWORD( wParam ) )
  179.               {
  180.  
  181.                  case IDM_ABOUT :
  182.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  183.                         break;
  184.  
  185.                  case IDM_EXIT :
  186.                         DestroyWindow( hWnd );
  187.                         break;
  188.               }
  189.               break;
  190.       
  191.       case WM_DESTROY :
  192.               PostQuitMessage(0);
  193.               break;
  194.  
  195.       default :
  196.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  197.    }
  198.  
  199.    return( 0L );
  200. }
  201.  
  202.  
  203. LRESULT CALLBACK About( HWND hDlg,           
  204.                         UINT message,        
  205.                         WPARAM wParam,       
  206.                         LPARAM lParam)
  207. {
  208.    switch (message) 
  209.    {
  210.        case WM_INITDIALOG: 
  211.                return (TRUE);
  212.  
  213.        case WM_COMMAND:                              
  214.                if (   LOWORD(wParam) == IDOK         
  215.                    || LOWORD(wParam) == IDCANCEL)    
  216.                {
  217.                        EndDialog(hDlg, TRUE);        
  218.                        return (TRUE);
  219.                }
  220.                break;
  221.    }
  222.  
  223.    return (FALSE); 
  224. }
  225.